package com.moleq.adapter;

import java.lang.reflect.Field;
import java.util.List;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.moleq.mgpos.activities.R;

public abstract class MQBaseAdapter2 extends BaseAdapter {

	protected int selectedRow = -1;
	protected boolean isEditable = false;
	protected MyTextWatcher mMyTextWatcher = new MyTextWatcher();
	private int mEditingResId = -1;
	private LayoutInflater mInflater;
	private Context mContext;
	private List<List<String>> mDataSource;
	private int mItemRenderer;

	public MQBaseAdapter2(Context context,int itemRenderer, List<List<String>> dataSource) {
		mInflater = LayoutInflater.from(context);
		mContext = context;
		mItemRenderer = itemRenderer;
		mDataSource = dataSource;
	}

	public void setItemRenderer(int itemRenderer) {
		mItemRenderer = itemRenderer;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mDataSource.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mDataSource.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		isEditable = false;

		List<String> columns = mDataSource.get(position);

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(mItemRenderer, null);
			holder.row = (LinearLayout) convertView.findViewById(R.id.ll_row);
			holder.view = new View[columns.size()];
			
			for (int i = 0; i < columns.size(); i++) {
				try {
					Field field = R.id.class.getField("column_" + i);
					int id = field.getInt(new R.id());

					holder.view[i] = convertView.findViewById(id);

					Log.d("MQBaseAdater2", i + "");
				} catch (Exception e) {
					Log.e("MQBaseAdater2", e.toString());
				}
			}

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		for (int j = 0; j < holder.view.length; j++) {
			Object newClass = holder.view[j];
			Class[] paramTypes= new Class[]{CharSequence.class};
			Object[] params = new Object[]{columns.get(j).trim()};
			String methodName = "setText";
			PrivateUtil.invoke(newClass, methodName,paramTypes , params);
		}

		return convertView;
	}

	public final class ViewHolder {
		public LinearLayout row;
		public View[] view;
	}

	@Override
	public void notifyDataSetChanged() {
		isEditable = false;
		super.notifyDataSetChanged();
	}

	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex() {
		return selectedRow;
	}

	abstract public void onTextChange(int resId, String changedText);

	protected OnTouchListener mOnTouchListener = new OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_UP) {
				selectedRow = (Integer) v.getTag();
				if (v instanceof EditText) {
					mEditingResId = v.getId();
					if (!isEditable) {
						isEditable = true;
					}
				}
			}

			return false;
		}
	};

	class MyTextWatcher implements TextWatcher {

		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (isEditable) {
				onTextChange(mEditingResId, s.toString());
			}
		}

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		public void afterTextChanged(Editable s) {

		}
	}

}
